<?php
namespace app\api\model;
use think\Db;
use think\Model;
/*聊天信息模型*/
class WxUser extends CommonModel
{

    public function __construct($args = [])
    {
        parent::__construct($args);
    }

    /**添加一个用户
     * @param $data
     * @return array|bool id和LoginToken
     */
    public function add($data)
    {
        $fields = [
            "openId" => "",
            "nickName"=>"匿名",
            "gender" => "0",
            "city"  => "",
            "country"=>"china",
            "province"=>"",
            "headImgUrl"=>"",
            "unionId"=>""
        ];

        //删除不该有的键与值
        $data = resetArr($data,array_keys($fields));
        if(empty($data['openId']))
        {
            return false;
        }
        $data = array_merge($fields,$data);
        $data['id'] = db('wx_user')->insertGetId($data);
        $data['LoginToken'] = $this->generateLoginToken(['openId'=>$data['openId'],'id'=>$data['id']]);
        $this->updateUser($data);
        return [
            'id' =>  $data['id'],
            'LoginToken'=>$data['LoginToken']
        ];
    }

    //更新一个用户的基本信息[除了openid和unionid]
    public function updateUser($data)
    {

        /**
         * 在更新用户信息之前，必须要生成新的token，否则导致token登录失败
         */
        $fields = [
            'id'      => "",
            'openId'  => "",
            'unionId' => "",
            "nickName"=>"匿名",
            "gender" => "0",
            "city"  => "",
            "country"=>"china",
            "province"=>"",
            "headImgUrl"=>"",
            "LoginToken"=>"",
            "user_id"   => ""
        ];
        //删除不该有的键与值
        $data = resetArr($data,array_keys($fields));
        //获取用户信息,根据id或者 openId和unionId查找

        $find = isset($data['id']) && !empty($data['id']) ? ['id'=>$data['id']] : ['openId'=>$data['openId'],'unionId'=>$data['unionId']];
        if(!$info = $this->info($find))
        {
            return false;
        }

        unset($data['id'],$data['openId'],$data['unionId']);

        db('wx_user')->where(['id'=>$info['id']])->update($data);
    }

    public function info($args)
    {
        /**
         * 参数说明 根据其中一个
         * args.openId && args.unionId
         * args.id
         */
        $where = [];
        if(isset($args['openId']) && isset($args['unionId']) && !empty($args['openId']))
        {
            $where = [
                'unionId' => $args['unionId'],
                'openId'  => $args['openId']
            ];
        }

       if(isset($args['id']) && !empty($args['id']))
        {
            $where['id'] = $args['id'];
        }
        if(isset($args['LoginToken']) && !empty($args['LoginToken']))
        {
            $where['LoginToken'] = $args['LoginToken'];
        }

        if(empty($where))
        {
            return false;
        }

        $data =  db('wx_user')->where($where)->find();

        if(!empty($data))
        {
            //头像初始化
            $photo_http = "http://";
            if(strpos($data['headImgUrl'],$photo_http) !== false)
            {
                $data['headImgUrl'] = str_replace($photo_http,"https://",$data['headImgUrl']);
            }
        }
        else
        {
            return false;
        }

        return $data;
    }

    public function testinfo($args)
    {
        /**
         * 参数说明 根据其中一个
         * args.openId && args.unionId
         * args.id
         */
        $where = [];
        if(isset($args['openId']) && isset($args['unionId']) && !empty($args['openId']))
        {
            $where = [
                'unionId' => $args['unionId'],
                'openId'  => $args['openId']
            ];
        }

        if(isset($args['id']) && !empty($args['id']))
        {
            $where['id'] = $args['id'];
        }
        if(isset($args['LoginToken']) && !empty($args['LoginToken']))
        {
            $where['LoginToken'] = $args['LoginToken'];
        }
        if(empty($where))
        {
            return false;
        }

        $data =  db('wx_user')->where($where)->find();

        if(!empty($data))
        {
            //头像初始化
            $photo_http = "http://";
            if(strpos($data['headImgUrl'],$photo_http) !== false)
            {
                $data['headImgUrl'] = str_replace($photo_http,"https://",$data['headImgUrl']);
            }
        }
        else
        {
            return false;
        }

        return $data;
    }

    public function generateLoginToken($args)
    {
        /**
         * 参数说明
         * args.id
         * args.openId
         */
        return sha1($args['id'].$args['openId'].randString(10));
    }

}